package gui;

import application.Mouvement;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.geom.Area;
import java.awt.geom.Point2D;

/**
 *
 * @author Laurent
 */
public class Projectile extends ElementDuJeu {

    Monde monde;
    private Tank tank;
    private Mouvement mouvement;
    private Image imgProjectile;
    private final double VITESSE_PROJECTILE = 20;
    private final double ANGLE_PROJECTILE;
    private double  i, j;    
    Point2D pointProjectile;
    int xBase, yBase, xPointDeDepart, yPointDeDepart;

    public Projectile(Tank tank, Monde monde, MouseEvent e) {
        this.tank = tank;
        this.monde = monde;
        
        ANGLE_PROJECTILE = Math.toRadians(tank.getAngleActuelCanon() + 90);
        
        xBase=tank.getX()+tank.getCanonX();
        yBase=tank.getY()+tank.getCanonY();
        xPointDeDepart=tank.getX()+tank.getBoutDuCanonCanonX();
        yPointDeDepart=tank.getY()+tank.getBoutDuCanonCanonY();        
        

        mouvement = new Mouvement();
        
        i=mouvement.getIJ(xBase, yBase, xPointDeDepart, yPointDeDepart)[0];        
        j=mouvement.getIJ(xBase, yBase, xPointDeDepart, yPointDeDepart)[1];
        
        this.setSize(12, 12);
        this.setLocation(xPointDeDepart, yPointDeDepart);
        do {
        imgProjectile = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("images/Projectile.png"));
        } while(imgProjectile.getHeight(monde) == -1 || imgProjectile.getWidth(monde) == -1);
        
        pointProjectile = new Point(xPointDeDepart, yPointDeDepart);
        
    }

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

        g2d.rotate(ANGLE_PROJECTILE, 6, 6);
        g2d.drawImage(imgProjectile, 4, 0, this);
    }

    public boolean checkValidity() {
        if (xPointDeDepart > monde.getWidth() || xPointDeDepart < 0 || yPointDeDepart > monde.getHeight() || yPointDeDepart < 0) {
            return false;
        } else {
            return true;
        }
    }

    public void bougerProjectile() {
        xPointDeDepart += i * VITESSE_PROJECTILE;
        yPointDeDepart += j * VITESSE_PROJECTILE;
        this.setLocation(xPointDeDepart, yPointDeDepart);
        
        pointProjectile = new Point(this.getX()+this.getWidth()/2, this.getY()+this.getHeight()/2);
        this.repaint();
    }
    
    
}
